﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using HHL_Acc.HHL_DaoAcc.DataBase;
using HHL_Acc.HHL_DaoAcc.DataObject;
using HHL_Acc.HHL_EntityAcc;

// ReSharper disable CheckNamespace

namespace HHL_DaoAcc.DataObject
// ReSharper restore CheckNamespace
{
    public class SmiImportDetailsDao : CBaseDao
    {
        public DataTable GetSmiImportDetails()
        {
            string sql =
                @"Select ROW_NUMBER() OVER (ORDER BY im.ImpID) as Idx,im.Imp_Number,im.Imp_Date, 
        im.Issue_Year, sim.ImpName,SUM(mid.Qty)as Qty,Deliver, (em.LASTNAME + em.FIRSTNAME),
        im.Notes 
        from Smi_Imports im inner join GEN_LOGINS lo on im.Login = lo.LOGIN and lo.DISABLED =0
        inner join GEN_EMPLOYEES em on lo.EMPID = em.EMPID and em.DISABLED = 0
        inner join Sys_ImpMethods sim on sim.ImpMethod = im.ImpMethod and sim.Disabled = 0
        inner join Smi_Import_Details mid on im.ImpID = mid.ImpID
        group by im.ImpID,im.Imp_Number,im.Imp_Date, 
        im.Issue_Year,sim.ImpName,Deliver,em.LASTNAME,em.FIRSTNAME,im.Notes
        ";
            DataSet ds = ExcuteSql(sql);
            if (ds != null)
            {
                return ds.Tables[0];
            }
            return null;
            //return list;
        }

        public IList<SmiImportDetails> GetSmiImportDetailByImpId(String impId)
        {
            string sql =
                @"SELECT ROW_NUMBER() OVER (ORDER BY d.ImpID)
 as Idx, d.ImpID ,d.SerialFrom ,d.TicketCode ,d.Qty as Quality  ,d.Status , tk.Price as Price
  FROM Smi_Import_Details d left join  SYS_TICKETCODES tk on d.TicketCode = tk.TicketCode
 where ImpID  = @ImpId";
            DataSet ds = ExcuteSql(sql, CommandType.Text, new SqlParameter("@ImpId", impId));
            if (ds != null)
            {
                return CDb.MapList<SmiImportDetails>(ds.Tables[0]);
            }
            return null;
        }

        public SmiImportDetails GetSmiImportDetail(String importId)
        {
            String sql = "Select * from Smi_Import_Details WHERE importId=@ImportID";
            DataSet ds = ExcuteSql(sql, CommandType.Text, new SqlParameter("@ImportID", importId));
            SmiImportDetails smiImportDetails = CDb.Map<SmiImportDetails>(ds.Tables[0]);
            return smiImportDetails;
        }

        public int InsertSmiImportDetail(SmiImportDetails smiImportDetails)
        {
            String sql =
                "INSERT Smi_Import_Details(ImpID,SerialFrom,TicketCode,Qty,Status) VALUES (@ImpID,@SerialFrom,@TicketCode,@Qty,@Status)";
            SqlParameter[] paramsSql = new SqlParameter[5];
            paramsSql[0] = new SqlParameter("@ImpID", smiImportDetails.ImpID);
            paramsSql[1] = new SqlParameter("@SerialFrom", smiImportDetails.SerialFrom);
            paramsSql[2] = new SqlParameter("@TicketCode", smiImportDetails.TicketCode);
            paramsSql[3] = new SqlParameter("@Qty", smiImportDetails.Quality);
            paramsSql[4] = new SqlParameter("@Status", smiImportDetails.Status);
            try
            {
                return ExcuteNoneQuery(sql, paramsSql);
            }
            catch
            {
                return -1;
            }
        }

        public int UpdateSmiImportDetail(SmiImportDetails smiImportDetails)
        {
            String sql =
                "Update Smi_Import_Details set SerialFrom = @SerialFrom,TicketCode = @TicketCode,Qty = @Qty,Status = @Status where ImpId = @ImpID";
            SqlParameter[] paramsSql = new SqlParameter[5];
            paramsSql[0] = new SqlParameter("@ImpID", smiImportDetails.ImpID);
            paramsSql[1] = new SqlParameter("@SerialFrom", smiImportDetails.SerialFrom);
            paramsSql[2] = new SqlParameter("@TicketCode", smiImportDetails.TicketCode);
            paramsSql[3] = new SqlParameter("@Qty", smiImportDetails.Quality);
            paramsSql[4] = new SqlParameter("@Status", smiImportDetails.Status);
            try
            {
                return ExcuteNoneQuery(sql, paramsSql);
            }
            catch
            {
                return -1;
            }
        }

        public int DeleteSmiImportDetailByImpId(String impId)
        {
            String sql = "Delete Smi_Import_Details where ImpId = @ImpID";
           
            try
            {
                return ExcuteNoneQuery(sql, new SqlParameter("@ImpID", impId));
            }
            catch
            {
                return -1;
            }
        }
    }
}